package cn.com.cetccst.compliancecheck.demo.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import cn.com.cetccst.compliancecheck.demo.R;

public class GeoFenceSetDlg extends AlertDialog.Builder {
    private View view;

    public static void show(Context context, Callback callback) {

        new GeoFenceSetDlg(context, callback).show().setCanceledOnTouchOutside(false);
    }

    private double getDoubleValue(int resId) {
        TextView textView = view.findViewById(resId);
        return Double.parseDouble(textView.getText().toString());
    }

    private float getFloatValue(int resId) {
        TextView textView = view.findViewById(resId);
        return Float.parseFloat(textView.getText().toString());
    }

    public GeoFenceSetDlg(@NonNull Context context, Callback callback) {
        super(context);
        view = LayoutInflater.from(context).inflate(R.layout.geo_fence_set, null, false);
        setView(view)
                .setPositiveButton("确定", (dialog, which) -> {
                    double lng = getDoubleValue(R.id.lng);
                    double lat = getDoubleValue(R.id.lat);
                    float radius = getFloatValue(R.id.radius);

                    callback.handleGeoFenceParams(lng, lat, radius);
                })
                .setNeutralButton("关闭围栏", (dialogInterface, i) -> callback.handleGeoFenceParams(0, 0, -1))
                .setNegativeButton("取消", null)
                .setCancelable(false);
    }

    public interface Callback {
        void handleGeoFenceParams(double lng, double lat, float radius);
    }
}
